package ch02;

public class SqList implements Ilist{//这是顺序表
    private Object[] listElem;
    private int curLen;

    public SqList(int maxSize){
        curLen = 0;
        listElem = new Object[maxSize];
    }
    public void clear(){
        curLen = 0;
    }
    public boolean isEmpty(){
        return curLen == 0;
    }
    public int length(){
        return curLen;
    }

    public Object get(int i) throws Exception{
        if (i<0||i>curLen-1)
            throw new Exception("第"+i+"个元素不存在");
        return listElem[i];
    }
    public void insert(int i, Object x)throws Exception {
        if (curLen == listElem.length)//判断顺序表是否已满
            throw new Exception("顺序表已满");        //抛出异常
        if (i < 0 || i > curLen) //i不合法
            throw new Exception("插入位置不合法");//抛出异常
        for (int j = curLen; j > i; j--)//插入位置及其之后的所有数据元素后移一位
            listElem[j] = listElem[j - i];//插入x
        listElem[i] = x;//表长加1
        curLen++;
    }
    public void remove(int i) throws Exception{
        if(i<0||i>curLen-1)
            throw new Exception("删除位置不合法");
        for (int j = i; j < curLen - 1;j++)
            listElem[j] = listElem[j+1];
        curLen--;
    }
    public int indexOf(Object x) {
        int j = 0;
        while (j < curLen && !listElem[j].equals(x))
            j++;
        if(j < curLen)
            return  j;
        else
            return - 1;
    }

    public void display() {
        for (int j = 0; j < curLen; j++)
            System.out.print(listElem[j] + "");
        System.out.println();
    }
}
